package com.framework.base.taglib.ui;

import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.framework.base.util.StringUtil;

/**
 * @Description:功能描述：国际化标签
 * 
 * @Company: www.51wuxia.com
 * @author：www.51wuxia.com
 */
@SuppressWarnings("serial")
public class i18nTag extends BodyTagSupport {
	private String name; // 国际化的key
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		
		String transValue="";
		try {
			ResourceBundle v=null;
			if(!StringUtil.isNullorEmpty(getName())){
				String[] values=getName().replace(".", "_").split("_");
				if (values.length!=1) {
					if(values[0].toLowerCase().equals("sys")){
						String up=values[1].substring(0, 1);
						String low=values[1].substring(1, values[1].length()).toLowerCase();	
						v = (ResourceBundle) pageContext.getServletContext().getAttribute(values[0]+"_"+values[1]);
						if (v==null) {
							v = ResourceBundle.getBundle("com.framework.main."+values[0].toLowerCase()+"."+values[1].toLowerCase()+".action."+up+low+"Action", Locale.getDefault()); 
							pageContext.getServletContext().setAttribute(values[0].toLowerCase()+"_"+values[1], v);
						}	
					}else{
						String up=values[1].substring(0, 1);
						String low=values[1].substring(1, values[1].length()).toLowerCase();	
						v = (ResourceBundle) pageContext.getServletContext().getAttribute(values[0]+"_"+values[1]);
						if (v==null) {
							v = ResourceBundle.getBundle("com.oa."+values[0].toLowerCase()+"."+values[1].toLowerCase()+".action."+up+low+"Action", Locale.getDefault()); 
							pageContext.getServletContext().setAttribute(values[0].toLowerCase()+"_"+values[1], v);
						}			
					}						
				}else {
					v = ResourceBundle.getBundle("globalMessages", Locale.getDefault()); 
				}				
				transValue=v.getString(getName());
			}
			out.write(transValue);
		} catch (Exception e) {
			e.printStackTrace();
			throw new JspException("错误信息: 国际化错误！");
		}		
		return super.doStartTag();
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	

	



}
